package com.commont;

import com.google.gson.*;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * 获取经纬度
 */
@Slf4j
public class BaiDuMapUtil {

    private static String AK="S6f3WsVzjamGU29k5A0vQsX4KjfxBzhT";

    /**
     * 查询出经纬度
     * @param address
     * @return
     */
    public static Map<String,Double> getLan(String address){
        Map<String,Double> map=new HashMap<String, Double>();
        String url = "http://api.map.baidu.com/geocoder/v2/?address="+address+"&output=json&ak="+AK+"";
        String json = loadJSON(url);
        JsonParser jsonParser=new JsonParser ();
        JsonObject jsonObject=jsonParser.parse ( json ).getAsJsonObject ();
        //log.info ( "【JsonObject】={}",jsonObject );
        //拆分数据
        if(jsonObject.get("status").toString().equals("0")){
            double lng=jsonObject.getAsJsonObject ( "result" ).getAsJsonObject ( "location" ).get ( "lng" ).getAsDouble ();
            double lat=jsonObject.getAsJsonObject ( "result" ).getAsJsonObject ( "location" ).get ( "lat" ).getAsDouble ();
            map.put("lng", lng);
            map.put("lat", lat);
        }else{
        }
        return map;
    }

    /**
     * 获取查询的地点
     * @param query
     * @param lat
     * @param lng
     * @param page
     * @param pageNum
     * @return
     */
    public static JsonObject getList(String query, Double lat, Double lng, Integer page, Integer pageNum ){

        String url = "http://api.map.baidu.com/place/v2/search?query="+query+"&location="+lat+","+lng+"&radius=2000&output=json&ak="+AK+"&page_num="+page+
                "&page_size="+pageNum+"&scope=2";
        //log.info ( "地址={}",url );
        String json = loadJSON(url);
        JsonParser jsonParser=new JsonParser ();
        JsonObject jsonObject= jsonParser.parse ( json ).getAsJsonObject ();
        //log.info ( "【jsonArray】={}",jsonObject );
        return jsonObject;

    }

    /**
     * 搜索检索
     * @param query
     * @return
     */
    public static JsonObject getListSuggestion(String query,String address){
        String url="http://api.map.baidu.com/place/v2/suggestion?query="+query+"&region="+address+"&city_limit=true&output=xml&ak="+AK+"";
        String json = loadJSON(url);
        log.info ( "ip={}",url);
        JsonParser jsonParser=new JsonParser ();
        JsonObject jsonObject= jsonParser.parse ( json ).getAsJsonObject ();
        log.info ( "数据信息={}",jsonObject );
        return jsonObject;

    }

    /**
     * 根据ip定位地址
     * @param ip
     * @return
     */
    public static String getAddressByIp(String ip){
        String url="http://api.map.baidu.com/location/ip?ip="+ip+"&ak="+AK+"&coor=bd09ll";
        String json = loadJSON(url);
        JsonParser jsonParser=new JsonParser ();
        JsonObject jsonObject= jsonParser.parse ( json ).getAsJsonObject ();
        //解析获取地址
        String address=jsonObject.getAsJsonObject ( "content" ).get ( "address" ).getAsString ();
        return address.substring ( 3,address.length () );
    }



    /**
     * 装换Json
     * @param url
     * @return
     */

    public static String loadJSON (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream(),"UTF-8"));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return json.toString ();
    }

    public static void main(String[] args){
        /*把代码中的ak值（红色字部分）更改为你自己的ak值，在百度地图API中注册一下就有。
        调用方式：*/
        Map<String,Double> map= BaiDuMapUtil.getLan ("南京易橙汇网络科技有限公司");
        System.out.println("经度："+map.get("lng")+"---纬度："+map.get("lat"));
    }

}

